/**给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。

进阶：你能尝试使用一趟扫描实现吗？***/


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* heador=new ListNode(0,head);
        ListNode* secondnode=heador;
        ListNode* firstnode=head;
        for(int i=0;i<n-1;i++)
        {
            firstnode=firstnode->next; 
        }
        while(firstnode->next!=nullptr)
        {
            firstnode=firstnode->next;
            secondnode=secondnode->next;
        }
        secondnode->next=secondnode->next->next;
        return heador->next;
        delete heador;
    }
};

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* heador=new ListNode(0,head);
        int length=1;
        while(head->next!=nullptr）
        {
            head=head->next;
            length++;
        }
        head=heador->next;
        ListNode* forword=heador;
        for(int i=1;i<length-n+1;i++)
        {
            head=head->next;
            forword=forword->next;
        }
        forword->next=head->next;
        return heador->next;
        delete heador;
    }
};


class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* heador=new ListNode(0,head);
        stack<ListNode*>stk;
        ListNode cur=heador;
        while(cur)
        {
            stk.push(cur);
        }
        
        for(int i=0;i<n-1;i++)
        {
            stk.pop();
        }
        cur=stk.top;
        cur->next=cur->next->next;
        return heador->next;
        delete heador;
    }
};


























